using Gtk;
using System;
using System.Data;
using System.Globalization;
using System.Reflection;

using Serpis;
using PArticulo;


public partial class MainWindow : Gtk.Window
{
	public MainWindow () : base(Gtk.WindowType.Toplevel)
	{
		Build ();
		
		refreshActions();
		
		notebook.SwitchPage += delegate 
		{
			refreshActions();
		};
		
		notebook.PageRemoved += delegate 
		{
			refreshActions();	
		};
		
		articuloAction.Activated += delegate 
		{
			ArticuloListView articuloListView = new ArticuloListView();
			articuloListView.Visible = true;
			
			notebook.AddTab(articuloListView);
			
			// me suscribo a los cambios del treeView
			articuloListView.Changed += delegate
			{
				editAction.Sensitive = articuloListView.HasSelected;
				deleteAction.Sensitive = articuloListView.HasSelected;
			};
		};
		
		categoriaAction.Activated += delegate 
		{
			CategoriaListView categoriaListView = new CategoriaListView();
			categoriaListView.Visible = true;
			notebook.AddTab(categoriaListView);
			
			categoriaListView.Changed += delegate
			{
				editAction.Sensitive = categoriaListView.HasSelected;
				deleteAction.Sensitive = categoriaListView.HasSelected;
					
			};				
		};		
		//miTreeView.Init(AppDbConnection.Instance.DbConnection, entrySelect.Text);
		
		findAction.Activated += delegate{
			currentListView.Refresh();		
		};
		
		newAction.Activated += delegate {
			currentListView.New();
		};
		
		editAction.Activated += delegate 
		{
			currentListView.Edit();
		};
		
		deleteAction.Activated += delegate {
			currentListView.Delete();	
		};
	}
	
	private void refreshActions()
	{
		IListView listView = currentListView;
		newAction.Sensitive = listView != null;
		editAction.Sensitive = listView != null && listView.HasSelected;
		deleteAction.Sensitive = listView != null && listView.HasSelected;
		findAction.Sensitive = listView != null;
	}
	// Esta propiedad (de solo lectura) nos devuelve el 
	private IListView currentListView
	{
		get {return notebook.CurrentPageWidget as IListView;}	
	}
	
	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}
}
